<?php
/**
 * Create by PhpStorm
 * User: mozongyou
 * Date: 2021/8/27
 * Time: 8:00 下午
 */
/**
 *
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 * @author mozongyou
 * @date 2021-08-27 8:00 下午
 */
declare(strict_types=1);

namespace App\Service\WebSocket;
//消息管理
class MessageService
{

    private static $_instance;

    private function __construct()
    {
    }

    public static function getInstance()
    {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 消息处理器
     * @param $fd
     * @param $data
     * @author mozongyou
     * @date 2021-08-27 10:00 下午
     */
    public function processor($fd, $data)
    {
        FdService::getInstance()->setHeartbeat($fd);
        $data = json_decode($data, true);
        if (!$routeValue = config('websocket-routes.' . $data['api'])) {
            return;
        }
        $className = substr($routeValue, 0, strpos($routeValue, '@'));
        $methodsName = substr($routeValue, strripos($routeValue, "@") + 1);
        make($className, ['enableCache' => 'true'])->$methodsName($fd, $data);
    }

    private function __clone()
    {
    }
}